<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class overview_plugin_toprated
{
	// ineo core
	var $core;
	// ipsclass
	var $ipsclass;
	
	// plugin data:
	var $plugin_data = array( 'tab_title' => 'Top Rated', 'is_tab' => true, 'tab_order' => 4, 'enabled' => true );
	
	/**
	* Constructor - Set variables, load libraries, etc.
	*/
	function overview_plugin_toprated( &$core )
	{
		// Set variables:
		$this->core     =& $core;
		$this->ipsclass =& $this->core->ipsclass;
		
		// Set lang-friendly title:
		$this->plugin_data['tab_title'] = $this->ipsclass->lang['ineo_optab_toprated'];
		
	}
	
	/**
	* Return plugin data to the core 
	*/
	function get_data()
	{
		// Load articles library if not already loaded:
		if( !isset( $this->core->libs['articles'] ) )
		{
			$this->core->load_library('articles');
		}
		// Query:
		$this->ipsclass->DB->query( $this->core->dblib->query_get_top_rated( $this->ipsclass->vars['ineo_index_count'] ) );
		
		$articles = '';
		
		// Build list using the template:
		while( $res = $this->ipsclass->DB->fetch_row() )
		{
			$total  = intval($res['rate_total']); 
			$count  = intval($res['rate_count']);
			
			if( $count == 0 )
			{
				$rating = 0;
			}
			else
			{
				$rating = round($total / $count,0);
			}
					
			$votes  = $count;
			
			
			$res['article_rating'] = $this->core->libs['global']->my_sprintf($this->ipsclass->lang['ineo_rating_text'],array('&rating' => $rating,'&votes' => $votes));
			
			$res['article_rating'] = $this->core->templates['global']->rating_stars( $rating , $res['article_rating'] );
			
			$res['url'] = $this->core->libs['global']->make_article_url( $res['article_id'], $res['article_text_id'] );
			
			$articles .= $this->core->templates['global']->recent_articles_entry( $res, 'article_rating' );
		}
		
		if( $this->plugin_data['is_tab'] )
		{
			$articles = array( 'output' => $articles );
			$articles['cols'] = array('article_title' => '85%','article_rating' => '15%');
			
			$articles = $this->core->templates['global']->tab_table( $articles );
		}
		else
		{
			$articles = $this->core->templates['global']->recent_articles($articles);
		}
		return $articles;
	}
}

?>